package com.zhihuichongcheng.application.ui.openclass.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.openclass.OpenClassBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.openclass.adapter.OpenCLassAdapter;
import com.zhihuichongcheng.application.util.RecycleViewDivider;

import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/11/10.
 */

public class OpenclassSearchActivity extends BaseActivity implements View.OnClickListener{

    private RecyclerView mRcvSearch;
    private OpenCLassAdapter mAdapter;
    private TextView mTvSearchTitle;
    private EditText mEtSearch;
    private ImageView mImDel;
    private TextView mTvCancel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordination_search);
        initView();
        initData();
    }

    private void initView()
    {
        mTvSearchTitle = (TextView)findViewById(R.id.tv_search_title);
        mTvSearchTitle.setText("搜索公开课");
        setStatusBarDarkMode();
        mTvCancel = (TextView)findViewById(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
        mEtSearch = (EditText)findViewById(R.id.et_search);
        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
                    filter(s.toString());
                    mTvSearchTitle.setVisibility(View.GONE);
                    mImDel.setVisibility(View.VISIBLE);
                    mRcvSearch.setVisibility(View.VISIBLE);
                }
                else
                {
                    mTvSearchTitle.setVisibility(View.VISIBLE);
                    mRcvSearch.setVisibility(View.GONE);
                    mImDel.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
        mImDel = (ImageView)findViewById(R.id.im_del);
        mRcvSearch = (RecyclerView) findViewById(R.id.rcv_search);
        mRcvSearch.addItemDecoration(new RecycleViewDivider(mContext,LinearLayoutManager.VERTICAL,2,getResources().getColor(R.color.color_d8d8d8)));
        mAdapter = new OpenCLassAdapter(mContext);
        mAdapter.setOnItemOnclick(new OnItemOnclick<OpenClassBean>() {
            @Override
            public void onClick(OpenClassBean openClassBean, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("title",openClassBean.getTitle());
                bundle.putString("id",openClassBean.getOpenClassId());
                startActivity(OpenClassDetailActivity.class,bundle);
            }
        });
//        mAdapter.setOnItemOnclick(new OnItemOnclick<CoordinationFileBean>() {
//            @Override
//            public void onClick(CoordinationFileBean coordinationFileBean, int position) {
//                int type = coordinationFileBean.getFileType();
//                if(type==1)
//                {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("title",coordinationFileBean.getFileName());
//                    bundle.putString("id",coordinationFileBean.getFileContentId());
//                    startActivity(CoordinationDetailActivity.class,bundle);
//                }
//                else
//                {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("title",coordinationFileBean.getFileName());
//                    bundle.putString("id",coordinationFileBean.getFileContentId());
//                    startActivity(CoordinationListActivity.class,bundle);
//                }
//            }
//        });
        mRcvSearch.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvSearch.setAdapter(mAdapter);
        mImDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtSearch.setText("");
            }
        });
    }

    private void initData()
    {
        mTvSearchTitle.setText("搜索文件夹，文件名");
    }

    private void filter(String str)
    {
        HashMap<String,String> params = new HashMap<>();
        params.put("token",mUser.getToken());
        params.put("title",str);
        mManager.post(NetworkAPI.Openclass_list, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                List<OpenClassBean> list = GsonUtil.paraeFromStringToList(value,OpenClassBean.class);
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String value) {

            }
        },params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_cancel:
                finish();
                break;
            default:
                break;
        }
    }


}
